package com.comleader.uap.service;

import static cn.hutool.core.date.DateUtil.formatDateTime;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.comleader.uap.common.response.Result;
import com.comleader.uap.common.util.ExcelUtil;
import com.comleader.uap.common.util.ResponseUtil;
import com.comleader.uap.mapper.CalledProtectPushMapper;
import com.comleader.uap.pojo.dto.FiveEffectStatDTO;
import com.comleader.uap.pojo.dto.OverrallIndicatorDTO;
import com.comleader.uap.pojo.entity.CalledProtectPush;
import com.comleader.uap.pojo.query.CallerProtectQuery;
import com.comleader.uap.pojo.query.FiveEffectQuery;
import com.comleader.uap.pojo.vo.CalledProtectDetailVO;
import com.comleader.uap.pojo.vo.CalledProtectListVO;

import cn.hutool.core.collection.CollUtil;

/**
 * @author lianxinjing
 * @description 针对表【api_called_protect_push】的数据库操作Service
 * @createDate 2024-11-23 16:02:12
 */
@DS("mysql")
@Service
public class CalledProtectPushService extends ServiceImpl<CalledProtectPushMapper, CalledProtectPush> {

    public List<CalledProtectListVO> selectList(CallerProtectQuery query) {
        if (CollUtil.isNotEmpty(query.getIssueTime())) {
            query.setIssueStartTime(query.getIssueTime().get(0));
            query.setIssueEndTime(query.getIssueTime().get(1));
        }
        List<CalledProtectListVO> list = baseMapper.selectList(query);
        return list;
    }

    public CalledProtectDetailVO selectDetail(String taskId, String commandId) {
        return baseMapper.selectDetail(taskId, commandId);
    }

    public void export(CallerProtectQuery query, HttpServletResponse response) throws IOException {
        List<Date> issueTime = query.getIssueTime();
        if (CollUtil.isEmpty(issueTime) || issueTime.size() != 2) {
            ResponseUtil.write(response, Result.fail("必须选择开始时间和结束时间"));
            return;
        }
        try {
            String fileName = "被叫/踢线保护_" + formatDateTime(issueTime.get(0)) + "_" + formatDateTime(issueTime.get(1)) + ".xlsx";
            List<CalledProtectListVO> list = selectList(query);
            ExcelUtil.exportMultipleSheet(response, fileName, CalledProtectListVO.class, "被叫踢线保护", list);
        } catch (Exception e) {
            log.error("被叫/踢线保护导出异常", e);
            ResponseUtil.write(response, Result.fail("系统异常"));
        }

    }

    /**
     * @Description 统计被叫保护下发量、撤销量，踢线保护下发量
     * @Auhtor 连新景
     * @Date 2024/12/03 16:57
     * @param query
     * @return com.comleader.uap.pojo.dto.OverrallIndicatorDTO.CalledProtect
     */
    public OverrallIndicatorDTO.CalledProtect countByActionStatus(FiveEffectQuery query) {
        return baseMapper.countByActionStatus(query);
    }

    public List<FiveEffectStatDTO.CalledProtect> countByDimension(FiveEffectQuery query) {
        return baseMapper.countByDimension(query);
    }

}
